home *** CD-ROM | disk | FTP | other *** search
/ PC Media 23 / PC MEDIA CD23.iso / share / prog / date50 / datedemo.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-15  |  13.2 KB  |  346 lines

  1. #include "datecl.h"
  2. #include <malloc.h>
  3. #include <iostream.h>
  4.  
  5. // Note:    Looking for memory leaks in Windows NT cannot be done by
  6. //            checking the available memory.  The of available memory,
  7. //            both the physical and virtual, is shared by all of the
  8. //            running processes.
  9. //            
  10. #if defined (_MSC_VER) && !defined(_WIN32)
  11.     #define MEM_LEFT    _memavl()
  12. #elif defined(__BORLANDC__) || defined(__TURBOC__)
  13.     #define MEM_LEFT    0L //farcoreleft()
  14. #else
  15.     #define MEM_LEFT    0L
  16. #endif
  17.  
  18. ////////////////////////////////////////////////////////////
  19. //    main for testing purposes only...
  20. ////////////////////////////////////////////////////////////
  21.  
  22. void test()
  23. {
  24.     cout << " Date Class 5.0 Demo \n\n";
  25.  
  26.     // Various versions of the constructors
  27.     // and various output
  28.     Date x(10,20,1962);
  29.     cout << x.formatDate(Date::FULL) << "\n";
  30.  
  31.     // constuctor with a string, just printing the day of the week
  32.     Date y="8/8/1988";
  33.     cout << y.formatDate(Date::DAY) << "\n";
  34.  
  35.     // constructor with a julian
  36.     Date z( 2450000L );
  37.     cout << z.formatDate(Date::FULL) << '\n';
  38.  
  39.     // using date addition and subtraction
  40.     Date a = x + 10;
  41.     cout << a.formatDate(Date::FULL) << '\n';
  42.     a = a - 25;
  43.     cout << a.formatDate(Date::EUROPEAN) << '\n';
  44.  
  45.     //using subtraction of two date objects
  46.     Date a1 = "7/13/1991";
  47.     Date a2 = a1 + 14;
  48.     cout << (a1-a2) << "\n";
  49.     cout << (a2+=10) << "\n";
  50.  
  51.     a1++;
  52.     cout << "Tommorrow= " << a1.formatDate(Date::FULL) << "\n";
  53.  
  54.     cout << "a1 (7-14-91) < a2 ("<< a2<< ")? ==> " << ((a1 < a2) ? "TRUE" : "FALSE") << "\n";
  55.     cout << "a1 (7-14-91) > a2 ("<< a2<< ")? ==> " << ((a1 > a2) ? "TRUE" : "FALSE") << "\n";
  56.  
  57.     cout << "a1 (7-14-91) < 8-01-91 ? ==> " << ((a1 < (Date)"08/01/1991") ? "TRUE" : "FALSE") << "\n";
  58.     cout << "a1 (7-14-91) > 8-01-91 ? ==> " << ((a1 > (Date)"08/01/1991") ? "TRUE" : "FALSE") << "\n";
  59.     cout << "a1 (7-14-91)== 7-14-91 ? ==> " << ((a1==(Date)"07/14/1991") ? "TRUE" : "FALSE") << "\n";
  60.     Date a3 = a1;
  61.     a1.formatDate(Date::MDY);
  62.  
  63.     cout << "a1 (" << a1 << ")== a3 (" << a3 << ") ? ==> " << ((a1==a3) ? "TRUE" : "FALSE") << "\n";
  64.     Date a4 = a1;
  65.     ++a4;
  66.     cout << "a1 ("<< a1<<")== a4 (" << a4 << ") ? ==> " << ((a1==a4) ? "TRUE" : "FALSE") << "\n";
  67.  
  68.     Date a5 = "today";
  69.     cout << "Today is: " << a5 << "\n";
  70.     a4 = "Today";
  71.     cout << "Today (a4) is: " << a4 << "\n";
  72.  
  73.     cout << "Today + 4 is: " << (a4+=4) << "\n";
  74.     a4 = "Today";
  75.     cout << "Today - 4 is: " << (a4-=4) << "\n";
  76.     cout << "=========== Leap Year Test ===========\n";
  77.     a1 = "1/15/1992";
  78.     cout << a1.formatDate(Date::FULL) << "\t" << ((a1.isLeapYear()) ? "Leap" : "non-Leap");
  79.     cout << "\t" << "day of year:  " << a1.DOY() << "\n";
  80.  
  81.     a1 = "2/16/1993";
  82.     cout << a1.formatDate(Date::FULL) << "\t" << ((a1.isLeapYear()) ? "Leap" : "non-Leap");
  83.     cout << "\t" << "day of year:  " << a1.DOY() << "\n";
  84.  
  85.     DOSDATE_T b0 = {15,02,1991,1};
  86.     Date b1 = b0;
  87.     cout << "=========== eom test ==============\n";
  88.     cout << "b1.eom() (s/b 2/28/91) ==> " << b1.eom() << "\n";
  89.  
  90. // #if !defined(_WIN32)    // This really doesn't work on Windows NT.
  91.     cout << "================== getDate test =====================\n";
  92.     DOSDATE_T ds = a1.getDate();
  93.     cout << "a1.getDate()  (s/b 2/16/1993) ==> " << ds << "\n";
  94. // #endif
  95.  
  96.     cout << "================== string assignment test ====================\n";
  97.     const char *date_string=a1;
  98.     cout << "a1 as a string (s/b 2/16/1993) ==> " << date_string << "\n";
  99.  
  100.     cout << "================== setFormat test ============================\n";
  101.     Date::setFormat(Date::FULL);
  102.     cout << "a1 (s/b FULL format) ==> " << a1 << "\n";
  103.     Date::setFormat(Date::EUROPEAN);
  104.     cout << "a1 (s/b EUROPEAN format) ==> " << a1 << "\n";
  105.     Date::setFormat(Date::COLLATE);
  106.     cout << "a1 (s/b COLLATE format) ==> " << a1 << "\n";
  107.  
  108.  
  109.     cout << "================== setOption test ============================\n";
  110.     cout << "Date abbreviation ON\n";
  111.     Date::setOption(Date::DATE_ABBR);
  112.     Date::setFormat(Date::MONTH);
  113.     cout << "a1 (s/b MONTH format) ==> " << a1 << "\n";
  114.     Date::setFormat(Date::DAY);
  115.     cout << "a1 (s/b DAY format) ==> " << a1 << "\n";
  116.     Date::setFormat(Date::FULL);
  117.     cout << "a1 (s/b FULL format) ==> " << a1 << "\n";
  118.     Date::setFormat(Date::EUROPEAN);
  119.     cout << "a1 (s/b EUROPEAN format) ==> " << a1 << "\n";
  120.     cout << "Century suppression ON\n";
  121.     Date::setOption(Date::NO_CENTURY);
  122.     Date::setFormat(Date::MDY);
  123.     cout << "a1 (s/b MDY format) ==> " << a1 << "\n";
  124.     cout << "Century suppression OFF\n";
  125.     Date::setOption(Date::NO_CENTURY,Date::OFF);
  126.     cout << "a1 (s/b MDY format) ==> " << a1 << "\n";
  127.     cout << "Century suppression ON\n";
  128.     Date::setOption(Date::NO_CENTURY);
  129.     cout << "a1 (s/b MDY format) ==> " << a1 << "\n";
  130.     Date::setFormat(Date::FULL);
  131.     cout << "a1 (s/b FULL format) ==> " << a1 << "\n";
  132.     Date::setOption(Date::DATE_ABBR,Date::OFF);
  133.     cout << "Century suppression OFF\n";
  134.     Date::setOption(Date::NO_CENTURY,Date::OFF);
  135.  
  136.     cout << "\n=============== Version 4.0 Enhancement Test =================\n";
  137.     
  138.     Date v4("11/26/1966");
  139.     cout << "\n---------- Set Stuff -----------\n";
  140.     cout << "First, 'Set' to today..." << "\n";
  141.     cout << "Before 'Set' => " << v4 << "\n";
  142.     cout << "After  'Set' => " << v4.Set() << "\n\n";
  143.  
  144.     cout << "Set to 11/26/66 => " << v4.Set(11,26,1966) << "\n";
  145.     cout << "Current Julian  => " << v4.julDate() << "\n";
  146.     cout << "Set to Julian 2450000L => " << v4.Set(2450000L) << "\n";
  147.     cout << "See! => " << v4.julDate() << "\n";
  148.  
  149.     cout << "---------- Add Stuff -----------\n";
  150.     cout << "Start => " << v4.Set() << "\n";
  151.     cout << "Add  4 Weeks  => " << v4.AddWeeks(4)    << "\n";
  152.     cout << "Sub 52 Weeks  => " << v4.AddWeeks(-52)  << "\n";
  153.     cout << "Add 21 Months => " << v4.AddMonths(21)  << "\n";
  154.     cout << "Sub 15 Months => " << v4.AddMonths(-15) << "\n";
  155.     cout << "Add  2 Years  => " << v4.AddYears(2)    << "\n";
  156.  
  157.     cout << flush;    
  158.  
  159. // TML
  160.     cout << "Set to 08/31/93 => " << v4.Set(8,31,1993) << endl;
  161.     cout << "Add  6 Months => " << v4.AddMonths(6)  << endl;
  162.  
  163.     cout << "---------- Misc Stuff -----------\n";
  164.     cout << "The date aboves' day of the month is => " << v4.Day() << endl;
  165.     cout << "There are " << v4.DaysInMonth() << " days in this month.\n";
  166.     cout << "The first day of this month lands on " << v4.FirstDOM() << endl;
  167.     cout << "This day happens to be " << v4.CDOW() << endl;
  168.     cout << "the " << v4.NDOW() << " day of the week," << endl;
  169.     cout << "on the " << v4.WOY() << " week of the year," << endl;
  170.     cout << "on the " << v4.WOM() << " week of the month, " << endl;
  171.     cout << "(which is " << v4.CMonth() << ")\n";
  172.     cout << "the "<< v4.NMonth() << "nth month in the year.\n";
  173.     cout << "The year alone is " << v4.NYear4() << endl;
  174.  
  175.     cout << "---------- First and Last Stuff -----------\n";
  176.     v4.Set();
  177.     cout << "The first date of this month is " << v4.BOM() << endl;
  178.     cout << "The last date of this month is " << v4.EOM() << endl;
  179.     cout << "The first date of this year is " << v4.BOY() << endl;
  180.     cout << "The last date of this year is " << v4.EOY() << endl;
  181.     cout << endl;
  182.  
  183.     cout << "\n=============== Version 4.2 Enhancement Test =================\n";
  184.     // TML - Memory test
  185.     cout << "*** Starting Memory " << (unsigned long) MEM_LEFT << "\n";
  186.  
  187.     Date *d1 = new Date("04/13/1967");
  188.     cout << "*d1  = " << d1->formatDate(Date::FULL) << "\n\n";
  189.     
  190.     Date     D2 = "7/4/1776";
  191.     int        I1 = 4;
  192.  
  193.     D2.setFormat(Date::FULL);
  194.  
  195.     cout << "Before: I1 = " << I1 << ",  D2 = " << D2 << endl;
  196.       cout << "---------- Postfix '++' test -----------\n";
  197.     cout << "Test : I1++ = " << I1++ << ",  D2++ = " << D2++ << endl;
  198.     cout << "After: I1   = " << I1 << ",  D2   = " << D2 << endl;
  199.  
  200.     cout << "---------- Prefix '++' test -----------\n";
  201.     cout << "Test : ++I1 = " << ++I1 << ",  ++D2 = " << ++D2 << endl;
  202.     cout << "After:   I1 = " << I1 << ",    D2 = " << D2 << endl;
  203.  
  204.     cout << "---------- Postfix '--' test -----------\n";
  205.     cout << "Test : I1-- = " << I1-- << ",  D2-- = " << D2-- << endl; 
  206.     cout << "After: I1   = " << I1 << ",  D2   = " << D2 << endl;
  207.  
  208.     cout << "---------- Prefix '--' test -----------\n";
  209.     cout << "Test : --I1 = " << --I1 << ",  --D2 = " << --D2 << endl;
  210.     cout << "After:   I1 = " << I1 << ",    D2 = " << D2 << endl;
  211.  
  212.  
  213.     cout << "---------- Testing the () operator -----------\n";
  214.     Date *d2=new Date;
  215.     cout << "d2's initial value: " << *d2 << "\n";
  216.     cout << "d1's current's buf: " << *d1 << "\n";
  217.  
  218.     d2->Set();
  219.     cout << "d2's value after 'Set()': " << *d2 << "\n";
  220.  
  221.     delete d2;
  222.     delete d1;
  223.  
  224.     cout << "\n=============== Version 4.3 Enhancement Test =================\n";
  225.     cout << "-------------- Testing BCE dates -------------\n";
  226.     Date d4="1/1/0";
  227.  
  228.     cout << "D4 (1/1/0)       = " << d4 << "\n";
  229.     cout << "D4 - 150 years   = " << d4.AddYears(-150) << "\n";
  230.     cout << "D4 + 150 years   = " << d4.AddYears(150) << "\n";
  231.  
  232.     cout << "--------- Testing String Assignment ----------\n";
  233.  
  234.     d4 = "23 March 1993";
  235.     d4.setFormat(Date::EUROPEAN);
  236.     cout << "EUROPEAN         : " << d4 << "\n";
  237.  
  238.     d4 = "3/23/1993";
  239.     d4.setFormat(Date::MDY);
  240.     cout << "MDY              : " << d4 << "\n";
  241.  
  242.     d4 = "Tuesday, March 23, 1993";
  243.     d4.setFormat(Date::FULL);
  244.     cout << "FULL             : " << d4 << "\n";
  245.  
  246.     d4 = "Tue, Mar 23, 1993";
  247.     d4.setOption(Date::DATE_ABBR);
  248.     cout << "FULL,ABBR.       : " << d4 << "\n";
  249.     cout << "\n";
  250.     d4.setOption(Date::DATE_ABBR, Date::OFF);
  251.  
  252.     d4 = "19930323";
  253.     d4.setFormat(Date::COLLATE);
  254.     cout << "COLLATE         : " << d4 << "\n";
  255.  
  256.  
  257.     d4 = "23 March 1993 B.C.E.";
  258.     d4.setFormat(Date::EUROPEAN);
  259.     cout << "EUROPEAN   B.C.E.: " << d4 << "\n";
  260.  
  261.     d4 = "Tuesday, March 23, 1993 B.C.E.";
  262.     d4.setFormat(Date::FULL);
  263.     cout << "FULL       B.C.E.: " << d4 << "\n";
  264.  
  265.     d4 = "Tue, Mar 23, 1993 B.C.E.";
  266.     d4.setOption(Date::DATE_ABBR);
  267.     cout << "FULL,ABBR. B.C.E.: " << d4 << "\n";
  268.     cout << "\n\n";
  269.  
  270.     cout << "\n=============== Version 5.0 Enhancement Test =================\n\n";
  271.                
  272.     cout << "Missing year test : Set d4 = \"7/4\";\n";
  273.     d4 = "7/4";
  274.     cout << "This should be 7/4/1993 ==>" << d4 << endl;
  275.     cout << "The Julian date for that is " << d4.julDate() << endl;
  276.     
  277.                
  278.     cout << "2-digit year test : Set d4 =\"7/4/76\";\n";
  279.     d4 ="7/4/76";
  280.     cout << "This should be 7/4/1976 ==>" << d4 << endl;
  281.  
  282.     cout << "Dot-date test : Set d4 =\".\";\n";
  283.     d4 = ".";
  284.     cout << "This should be today ==>" << d4 << endl;
  285.     
  286.     int    nYear = d4.NYear4();
  287.     
  288.     cout << "Holiday tests\n";
  289.     cout << "This year  : \n";
  290.                         
  291.     cout << "NewYearsDay(nYear)         == " << Date::NewYearsDay(nYear) << '\n';    
  292.     cout << "PresidentsDay(nYear)       == " << Date::PresidentsDay(nYear) << '\n';
  293.     cout << "ValentinesDay(nYear)       == " << Date::ValentinesDay(nYear)    << '\n';
  294.     cout << "StPatricksDay(nYear)       == " << Date::StPatricksDay(nYear)    << '\n';
  295.     cout << "MothersDay(nYear)          == " << Date::MothersDay(nYear)    << '\n';
  296.     cout << "MemorialDay(nYear)         == " << Date::MemorialDay(nYear)    << '\n';
  297.     cout << "FlagDay(nYear)             == " << Date::FlagDay(nYear)        << '\n';
  298.     cout << "FathersDay(nYear)          == " << Date::FathersDay(nYear)    << '\n';
  299.     cout << "CanadaDay(nYear)           == " << Date::CanadaDay(nYear)        << '\n';
  300.     cout << "IndependenceDay(nYear)     == " << Date::IndependenceDay(nYear)<< '\n';
  301.     cout << "BastilleDay(nYear)         == " << Date::BastilleDay(nYear)    << '\n';
  302.     cout << "LaborDay(nYear)            == " << Date::LaborDay(nYear)        << '\n';
  303.     cout << "VeteranDay(nYear)          == " << Date::VeteransDay(nYear)    << '\n';
  304.     cout << "ThanksgivingDay(nYear)     == " << Date::ThanksgivingDay(nYear)<< '\n';
  305.     cout << "ChristmasDay(nYear)        == " << Date::ChristmasDay(nYear)    << '\n';
  306.  
  307.     Date d5= Date(0,Date::SATURDAY,Date::SEPTEMBER,nYear);
  308.     cout << "\nThe Last Saturday in September is : " << d5 << '\n';
  309.  
  310.     cout << "\n=============== Wife test. (Just to make her happy) =================\n\n";
  311.     Date bday;
  312.     Date::setOption(Date::DATE_ABBR, Date::OFF);
  313.     cout << "Raymond was born on "   << Date("7/11/1961").formatDate(Date::FULL) << '\n';
  314.     cout << "Catherine was born on " << Date("8/24/63"  ).formatDate(Date::FULL) << '\n';
  315.     cout << "Danielle was born on "  << Date("8/5/1982" ).formatDate(Date::FULL) << '\n';
  316.     cout << "Joseph was born on "    << Date("8/15/1984").formatDate(Date::FULL) << '\n';
  317.     cout << "Nicole was born on "    << Date("9/18/1986").formatDate(Date::FULL) << '\n';
  318.     cout << "Benjamin was born on "  << Date("4/25/1988").formatDate(Date::FULL) << '\n';
  319.  
  320. #if !defined(_WIN32)
  321.     cout << "*** Ending Memory " << (unsigned long) MEM_LEFT << "\n\n";
  322. #endif
  323. }
  324.  
  325.  
  326. int main()
  327.     {
  328.  
  329.     for (int i = 1; i < 2; i++)
  330.         {
  331. #if !defined(_WIN32)
  332.         unsigned long t1, t2;
  333.         cout << "*** Starting Program Memory " ;
  334.         cout << (t1=(unsigned long) MEM_LEFT) << "\n";
  335. #endif
  336.         test();
  337. #if !defined(_WIN32)
  338.         cout << "*** Ending Program Memory ";
  339.         cout << (t2=(unsigned long) MEM_LEFT) << "\n";
  340.  
  341.         cout << "\nProgram Memory Difference: " << (long)(t2 - t1) << "\n\n\n\n";
  342. #endif
  343.         }
  344.     return(0);
  345.     };
  346.